home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / gui / pref / prefsdialog.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  15KB  |  407 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. import wx
  6. from wx import WHITE_BRUSH, TRANSPARENT_PEN, FONTWEIGHT_BOLD
  7. from gui.textutil import default_font
  8. from gui.toolbox import build_button_sizer, snap_pref, persist_window_pos, AutoDC, calllimit
  9. from gui.validators import LengthLimit
  10. from gui.pref.prefstrings import all as allprefstrings, tabnames
  11. from util import import_function, Delegate, traceguard
  12. from common import profile
  13. from logging import getLogger
  14. log = getLogger('prefsdialog')
  15. prefstrings = dict((lambda .0: for name, module in .0:
  16. (name, ' '.join((lambda .0: for s in .0:
  17. s.lower().replace('&', ''))(module.itervalues())))
  18.     
  19. )(allprefstrings.iteritems()))
  20. EXPAND_ALL = wx.EXPAND | wx.ALL
  21. wxMac = 'wxMac' in wx.PlatformInfo
  22.  
  23. def show(tabname):
  24.     if not isinstance(tabname, str):
  25.         raise TypeError('prefsdialog.show takes a tab name')
  26.     
  27.     tabindex = [ c[0] for c in tabnames ].index(tabname)
  28.     return show_prefs_window(None, tabindex)
  29.  
  30.  
  31. def show_prefs_window(parent, tab = 0):
  32.     win = PrefsDialog.RaiseExisting()
  33.     if win is not None:
  34.         win.show_tab(tab)
  35.         return win
  36.     
  37.     pdiag = PrefsDialog(None, initial_tab = tab)
  38.     persist_window_pos(pdiag, position_only = True)
  39.     wx.CallAfter(pdiag.Show)
  40.     return pdiag
  41.  
  42.  
  43. class PrefsSearch(wx.SearchCtrl):
  44.     
  45.     def __init__(self, parent):
  46.         wx.SearchCtrl.__init__(self, parent, -1, style = wx.TE_PROCESS_ENTER, validator = LengthLimit(128))
  47.         self.ShowSearchButton(True)
  48.  
  49.  
  50.  
  51. class PrefsTabs(wx.VListBox):
  52.     item_height = 27
  53.     
  54.     def __init__(self, parent):
  55.         wx.VListBox.__init__(self, parent, size = (130, -1))
  56.         self.ItemCount = len(tabnames)
  57.         self.spotlight_indices = set()
  58.         self.Bind(wx.EVT_MOTION, self.OnMotion)
  59.         self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeave)
  60.         self.Bind(wx.EVT_KEY_DOWN, self.OnKey)
  61.         self.UpdateSkin()
  62.         self.tab_indices = dict((lambda .0: for modname, nicename in .0:
  63. (modname, i))(enumerate(tabnames)))
  64.         self.hoveridx = -1
  65.  
  66.     
  67.     def OnKey(self, e):
  68.         e.Skip()
  69.         if e.KeyCode == wx.WXK_ESCAPE:
  70.             self.Top.Close()
  71.         
  72.  
  73.     
  74.     def OnLeave(self, e):
  75.         self.Hover = -1
  76.         e.Skip(True)
  77.  
  78.     
  79.     def OnMotion(self, e):
  80.         self.Hover = self.HitTest(e.Position)
  81.         e.Skip(True)
  82.  
  83.     
  84.     def get_hover(self):
  85.         return self.hoveridx
  86.  
  87.     
  88.     def set_hover(self, i):
  89.         old = self.hoveridx
  90.         self.hoveridx = i
  91.         if i != old:
  92.             if old != -1:
  93.                 self.RefreshLine(old)
  94.             
  95.             if i != -1:
  96.                 self.RefreshLine(i)
  97.             
  98.         
  99.  
  100.     Hover = property(get_hover, set_hover)
  101.     
  102.     def UpdateSkin(self):
  103.         skin = skin
  104.         import gui
  105.         self.bgs = skin.get('AppDefaults.preftabs.backgrounds')
  106.         self.BackgroundColour = wx.Colour(*self.bgs.normal)
  107.  
  108.     
  109.     def tabname(self, index):
  110.         return tabnames[index][1]
  111.  
  112.     
  113.     def spotlight(self, tab_names):
  114.         old = self.spotlight_indices
  115.         self.spotlight_indices = (set,)((lambda .0: for name in .0:
  116. self.tab_indices[name])(tab_names))
  117.         self.RefreshAll()
  118.  
  119.     
  120.     def OnMeasureItem(self, n):
  121.         return self.item_height
  122.  
  123.     
  124.     def OnDrawItem(self, dc, rect, n):
  125.         selected = self.GetSelection() == n
  126.         font = default_font()
  127.         if selected:
  128.             font.SetWeight(FONTWEIGHT_BOLD)
  129.         
  130.         fh = font.Height
  131.         dc.Font = font
  132.         dc.TextForeground = wx.BLACK
  133.         pt = wx.Point(*rect[:2]) + (5, self.item_height / 2 - fh / 2)
  134.         dc.DrawText(self.tabname(n), *pt)
  135.  
  136.     
  137.     def OnDrawBackground(self, dc, rect, n):
  138.         selected = self.Selection == n
  139.         hover = self.Hover == n
  140.         dc.Pen = TRANSPARENT_PEN
  141.         if n in self.spotlight_indices:
  142.             self.bgs.search.Draw(dc, rect, n)
  143.         elif selected:
  144.             self.bgs.selected.Draw(dc, rect, n)
  145.         elif hover:
  146.             self.bgs.hover.Draw(dc, rect, n)
  147.         else:
  148.             self.bgs.normal.Draw(dc, rect, n)
  149.  
  150.  
  151. prefs_dialog_style = wx.DEFAULT_FRAME_STYLE & ~(wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX)
  152.  
  153. class PrefsDialog(wx.Frame):
  154.     border = 5
  155.     default_selected_tab = 1
  156.     default_size = (700, 525)
  157.     
  158.     def __init__(self, parent, initial_tab = default_selected_tab):
  159.         wx.Frame.__init__(self, parent, title = _('Digsby Preferences'), size = self.default_size, style = prefs_dialog_style, name = 'Preferences Window')
  160.         self.loaded_panels = { }
  161.         self.SetMinSize(self.default_size)
  162.         self.create_gui()
  163.         self.bind_events()
  164.         self.layout_gui()
  165.         self.exithooks = Delegate()
  166.         traceguard.__enter__()
  167.         
  168.         try:
  169.             skin = skin
  170.             import gui
  171.             self.SetFrameIcon(skin.get('AppDefaults.TaskbarIcon'))
  172.         finally:
  173.             pass
  174.  
  175.         if not wxMac:
  176.             self.BackgroundColour = wx.WHITE
  177.             self.Bind(wx.EVT_ERASE_BACKGROUND, (lambda e: pass))
  178.         
  179.         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
  180.         self.Bind(wx.EVT_PAINT, self.OnPaint)
  181.         self.tabs.SetSelection(initial_tab)
  182.         self.on_tab_selected(initial_tab)
  183.         self.Bind(wx.EVT_CLOSE, self.on_close)
  184.         self._loaded = 0
  185.         snap_pref(self)
  186.         profile.prefs.add_observer(self.incoming_network_prefs)
  187.         KeyCatcher = KeyCatcher
  188.         import gui.uberwidgets.keycatcher
  189.         k.OnDown('ctrl+w', self.Close)
  190.         k.OnDown('escape', self.Close)
  191.  
  192.     
  193.     def OnPaint(self, e):
  194.         dc = AutoDC(self)
  195.         crect = self.ClientRect
  196.         if not wxMac:
  197.             dc.Brush = WHITE_BRUSH
  198.             dc.Pen = TRANSPARENT_PEN
  199.             dc.DrawRectangleRect(crect)
  200.         
  201.         self.tabs.bgs.normal.Draw(dc, wx.Rect(0, 0, self.tabs.ClientRect.width, crect.height))
  202.  
  203.     
  204.     def incoming_network_prefs(self, src, *a):
  205.         wx.CallAfter(self._gui_incoming_network_prefs, src)
  206.  
  207.     
  208.     def _gui_incoming_network_prefs(self, src):
  209.         if src.isflagged('network'):
  210.             wx.CallAfter(self.reload)
  211.         
  212.  
  213.     _gui_incoming_network_prefs = calllimit(2)(_gui_incoming_network_prefs)
  214.     
  215.     def on_close(self, e):
  216.         self.Hide()
  217.         e.Skip()
  218.         profile = profile
  219.         import common
  220.         profile.prefs.remove_observer(self.incoming_network_prefs)
  221.         self.exithooks()
  222.         for panel in self.loaded_panels.itervalues():
  223.             if hasattr(panel, 'on_close'):
  224.                 traceguard.__enter__()
  225.                 
  226.                 try:
  227.                     panel.on_close()
  228.                 finally:
  229.                     pass
  230.  
  231.                 continue
  232.             traceguard
  233.         
  234.         profile.save('prefs')
  235.         profile.save('notifications')
  236.         memory_event = memory_event
  237.         import gui.native
  238.         memory_event()
  239.  
  240.     
  241.     def create_gui(self):
  242.         self.tabs = PrefsTabs(self)
  243.         self.search = PrefsSearch(self)
  244.         self.search.Bind(wx.EVT_TEXT, self.search_text)
  245.         self.search.Bind(wx.EVT_TEXT_ENTER, self.search_enter)
  246.         if not wxMac:
  247.             self.save_button = wx.Button(self, wx.ID_SAVE, _('&Done'))
  248.             self.save_button.SetDefault()
  249.             self.save_button.Bind((wx.EVT_BUTTON,), (lambda e: self.Close()))
  250.         
  251.  
  252.     
  253.     def search_enter(self, e):
  254.         if len(self.tabs.spotlight_indices) == 1:
  255.             i = self.tabs.spotlight_indices.copy().pop()
  256.             self.tabs.SetSelection(i)
  257.             self.on_tab_selected(i)
  258.         
  259.  
  260.     
  261.     def search_text(self, e):
  262.         wx.CallAfter(self._search)
  263.  
  264.     
  265.     def _search(self):
  266.         val = self.search.Value
  267.         if val == 'Search':
  268.             return None
  269.         
  270.         val = val.lower()
  271.         if val == '':
  272.             return self.tabs.spotlight([])
  273.         
  274.         tab_highlights = set()
  275.         for module_name, stringset in prefstrings.iteritems():
  276.             if val in stringset:
  277.                 tab_highlights.add(module_name)
  278.                 continue
  279.         
  280.         self.tabs.spotlight(tab_highlights)
  281.  
  282.     
  283.     def bind_events(self):
  284.         self.tabs.Bind(wx.EVT_LISTBOX, self.on_tab_selected)
  285.  
  286.     
  287.     def layout_gui(self):
  288.         self.content_sizer = wx.BoxSizer(wx.VERTICAL)
  289.         hz = wx.BoxSizer(wx.HORIZONTAL)
  290.         hz.Add(self.build_tab_sizer(), 0, EXPAND_ALL)
  291.         hz.Add(self.content_sizer, 1, EXPAND_ALL)
  292.         v = wx.BoxSizer(wx.VERTICAL)
  293.         v.Add(hz, 1, EXPAND_ALL)
  294.         if not wxMac:
  295.             v.Add(build_button_sizer(self.save_button, border = self.border), 0, wx.EXPAND | wx.SOUTH | wx.EAST, 4)
  296.         
  297.         self.Sizer = v
  298.  
  299.     
  300.     def build_tab_sizer(self):
  301.         sz = wx.BoxSizer(wx.VERTICAL)
  302.         sz.Add(self.search, 0, EXPAND_ALL, self.border)
  303.         if wxMac:
  304.             sz.AddSpacer(6)
  305.         
  306.         sz.Add(self.tabs, 1, EXPAND_ALL)
  307.         return sz
  308.  
  309.     
  310.     def on_tab_selected(self, e):
  311.         index = None if isinstance(e, int) else e.Int
  312.         if index != -1:
  313.             self.tabs.Update()
  314.             wx.CallAfter(self.show_panel, self.panel_for_tab(index))
  315.         
  316.  
  317.     
  318.     def show_tab(self, n):
  319.         log.info('show_tab %d', n)
  320.         self.tabs.SetSelection(n)
  321.         self.show_panel(self.panel_for_tab(n))
  322.  
  323.     
  324.     def reload(self):
  325.         log.info('reloading Prefs dialog')
  326.         self.Frozen().__enter__()
  327.         
  328.         try:
  329.             child = self.content_sizer.Children[0]
  330.             child.Show(False)
  331.             self.content_sizer.Detach(0)
  332.             for panel in self.loaded_panels.itervalues():
  333.                 
  334.                 try:
  335.                     panel.on_close()
  336.                 except AttributeError:
  337.                     self.Frozen()
  338.                     self.Frozen()
  339.                 except:
  340.                     self.Frozen()
  341.  
  342.                 panel.Destroy()
  343.             
  344.             self.loaded_panels.clear()
  345.             del self.exithooks[:]
  346.             self.show_tab(self.tabs.GetSelection())
  347.         finally:
  348.             pass
  349.  
  350.  
  351.     
  352.     def show_panel(self, panel):
  353.         if not isinstance(panel, wx.WindowClass):
  354.             raise TypeError('show_panel takes a Window, you gave %r' % panel)
  355.         
  356.         self.FrozenQuick().__enter__()
  357.         
  358.         try:
  359.             s = self.content_sizer
  360.             if len(s.GetChildren()) > 0:
  361.                 s.Show(0, False)
  362.                 s.Detach(0)
  363.             
  364.             s.Add(panel, 1, EXPAND_ALL, 10)
  365.             self.Layout()
  366.         finally:
  367.             pass
  368.  
  369.         wx.CallAfter(panel.Show)
  370.  
  371.     
  372.     def panel_for_tab(self, i):
  373.         module_name = tabnames[i][0]
  374.         if module_name not in self.loaded_panels:
  375.             log.info('loading panel "%s"', module_name)
  376.             func = import_function('gui.pref.pg_%s.panel' % module_name)
  377.             panel = self._construct_sub_panel(func)
  378.             self.loaded_panels[module_name] = panel
  379.         
  380.         return self.loaded_panels[module_name]
  381.  
  382.     
  383.     def _construct_sub_panel(self, func):
  384.         PrefsPanel = PrefsPanel
  385.         import gui.pref.prefcontrols
  386.         p = PrefsPanel(self)
  387.         p.Sizer = sz = wx.BoxSizer(wx.VERTICAL)
  388.         szAdd = sz.Add
  389.         PrefPanel = PrefPanel
  390.         PrefCollection = PrefCollection
  391.         import gui.uberwidgets.PrefPanel
  392.         
  393.         def addgroup(titleortuple, *workers, **options):
  394.             if isinstance(titleortuple, tuple):
  395.                 (title, prefix) = titleortuple
  396.             else:
  397.                 title = titleortuple
  398.                 prefix = ''
  399.             group = PrefCollection(*workers, **options)
  400.             panel = PrefPanel(p, group, title, prefix = prefix)
  401.             szAdd(panel, 0, EXPAND_ALL, 3)
  402.             return panel
  403.  
  404.         return func(p, sz, addgroup, self.exithooks)
  405.  
  406.  
  407.